<?php
//Rquire MySqlAdapter.
require_once(dirname(__FILE__).'/../db/MySqlAdapter.php');

//get the q parameter from URL
$q=$_GET["query"];
$response = "{"."query:'$q',"."suggestions:[";

$conn = MySqlAdapter::connect1();
if ($conn):
	$result = MySqlAdapter::connect2();
else:
	die ( $response."'"."suggestions: Could not connect to db ". mysql_error()."'"." ],}");
endif;

if(!$result){
	$response.= "'"."suggestions: Could not find DataBase"."'"." ],}";
} else{
	//echo "<br>We found an existing table  in your DB.";

	$res = mysql_query("SELECT name FROM equipment WHERE name LIKE '%".$q."%' ORDER BY name DESC");
	//$res = mysql_query("select first_name from person where first_name like '%".$q."%' ORDER BY first_name desc");
	
	$counter=0;
	for ($counter=0; $row = mysql_fetch_array($res); $counter++)
	{
		if ($counter > 0) {
			$response.= ",";
		}
		$name=$row["name"];
		$response.= "'$name'";
	}

	if ($counter == 0){
	  $response.= '\''."no suggestion".'\''." ";
	}
	  
	$response.= "],}";
  }
  
//close connection with the Data Base
mysql_close($conn);

//output the response
echo $response;
?>